#include "atsha204_device_configuration.h" 
#include "sha204_physical.h"  
#include "atsha204_defines.h"
#include "sha204_comm.h"
#include "sha204_helper.h"
#include "sha204_lib_return_codes.h"
#include "software_timer_utilities.h"
#include "atsha204_read_sn.h"
#include "atsha204_mac.h"
#include "i2c_phys.h" 
#include "atsha204_enc_read.h"
#include "atsha204_enc_write.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "sha204_usr.h"

#define DEVICE_ADDR 0XC8

uint8_t txbuf[64] = {0};
uint8_t rxbuf[64] = {0};
void printf_array(char *ptr_array_name,uint8_t *ptr_buffer,uint8_t buffer_size)
{
  uint8_t temp = 0;

  for(temp = 0; temp < buffer_size; temp++)
  {
    printf(ptr_array_name);    
    printf("[%d]=0x%2x",temp,ptr_buffer[temp]);
   	 printf(", "); 
    if(((temp + 1)%4 == 0)&&(temp != 0))
    {//没显示4个16进制的数就换行
      printf("\r\n");       
    }         
  }
	if(temp % 4 !=0)
	{//最后一行不足4个，换行
		printf("\r\n"); 		
	}
}

void ATSHA204A_START(void)
{
		char stat = 0;
		uint8_t sha204_lib_return = SHA204_SUCCESS;
		uint8_t serial_number[9] = {0};//sn
		uint8_t wakeup_response_buffer[4] = {0};
    static uint8_t digest[32] = {0};  //保存MCU计算出来的摘要
		uint8_t buffer[4] = {0x80,0xa0,0x80,0xa0};
		uint8_t data1[32]={0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22};
		static uint8_t message1[]= {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x00,0x00, 
		0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22, 
		0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xee,0x00,0x00,0x00,0x00,0x01,0x23,0x00, 0x00,};
		sha204p_init();
			
/*******START**********/		
		printf("\r\n");	
		printf("----- ATSHA204A function base on NUC505-----!\r\n");
		printf("\r\n");	
/******wake up********/
	software_delay_ms(10);	
	printf("-ATSHA204A function:Wake-up ATASHA204A--BEGIN!\r\n");
	sha204_lib_return = sha204c_wakeup(wakeup_response_buffer);
	if(SHA204_SUCCESS != sha204_lib_return)
	{
		printf("-ATSHA204A function:Wake-up ATASHA204A--FALIED!\r\n");		
	}
	else
	{
		printf("-ATSHA204A function:Wake-up ATASHA204A--SUCESS!\r\n");	
		printf_array("buff", wakeup_response_buffer, 4);	
	}
	sha204p_sleep();	
					
	printf("\r\n");	

	
/*******Read ATSHA204 serial number*****/
	software_delay_ms(10);
	printf("--ATSHA204A function:Read ATSHA204A serial number!\r\n");	
	sha204_lib_return = atsha204_read_sn(serial_number);
	if(SHA204_SUCCESS!=sha204_lib_return)
	{
		printf("----Read ATSHA204A serial number: FAILED!!\r\n");			
	}
	printf("----Read ATSHA204A serial number: SUCCESS!!\r\n");	
	printf_array("SN", serial_number, sizeof(serial_number));	
	printf("\r\n");	
	/******************MAC************/
	config();
	sha204m_execute(0x17, 0x80, 0x00,0, 0, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf); 
	config1();
	sha204m_execute(0x17, 0x81, 0x00,0, 0, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf); 
	sha204m_execute(0x08,0x00,0x0000,32, data1, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf);
	sha204h_calculate_sha256(88,message1,digest);
	int i = memcmp(digest  ,rxbuf + 1,30);
	if( i == 0) 
	{
		printf("加密成功!\n");
		
	}
	else
		printf("加密失败!\n");
		
}
		

void config(void) 
{ 
	uint8_t data1[4]={0x80,0x80,0x80,0x80}; 
	uint8_t data2[4]={0x80,0x00,0x80,0x00}; 
	uint8_t data3[4]={0x00,0x00,0x00,0x00};
	uint8_t data4[4]={0xC0,0x80,0xC0,0x80};
	uint8_t data5[4]={0xC0,0x00,0xC0,0x00};

	sha204m_execute(0x12, 0x00, 0x05,4, data1, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf); 
	sha204m_execute(0x12, 0x00, 0x06,4, data2, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf); 
	sha204m_execute(0x12, 0x00, 0x07,4, data3, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf); 
	sha204m_execute(0x12, 0x00, 0x08,4, data4, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf); 
	sha204m_execute(0x12, 0x00, 0x09,4, data5, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf); 
	sha204m_execute(0x12, 0x00, 0x0A,4, data3, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf); 
	sha204m_execute(0x12, 0x00, 0x0B,4, data3, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf); 
	sha204m_execute(0x12, 0x00, 0x0C,4, data3, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf);

}
void config1(void)
{
	uint8_t data1[32]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x00,0x00}; 
	uint8_t data3[32]={0x55,0x55,0x55,0x55,0x55,0x55,0x55,0xAA,0xAA,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x55,0x55,0xAA};
	uint8_t data4[32]={0x11,0x11,0x23,0xB6,0xCC,0x53,0xB7,0xB9,0xE9,0xBB,0x51,0xFD,0x2F,0x74,0xCD,0x0E,0x91,0xD9,0x7F,0xEB,0x84,0x7B,0x98,0x09,0xF4,0xCD,0x93,0x6A,0xB6,0x48,0x11,0x11}; 
	sha204m_execute(0x12, 0x82, 0x0010,32, data1, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf); 
	sha204m_execute(0x12, 0x82, 0x0000,32, data1, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf);  
	sha204m_execute(0x12, 0x82, 0x0008,32, data1, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf);  
	sha204m_execute(0x12, 0x82, 0x0010,32, data1, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf); 
	sha204m_execute(0x12, 0x82, 0x0018,32, data1, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf); 
	sha204m_execute(0x12, 0x82, 0x0020,32, data1, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf);  
	sha204m_execute(0x12, 0x82, 0x0028,32, data1, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf); 
	sha204m_execute(0x12, 0x82, 0x0030,32, data1, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf); 
	sha204m_execute(0x12, 0x82, 0x0038,32, data1, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf); 
	sha204m_execute(0x12, 0x82, 0x0040,32, data1, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf);  
	sha204m_execute(0x12, 0x82, 0x0048,32, data1, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf); 
	sha204m_execute(0x12, 0x82, 0x0050,32, data1, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf); 
	sha204m_execute(0x12, 0x82, 0x0058,32, data1, 0, 0, 0, 0,SHA204_CMD_SIZE_MIN, txbuf,WRITE_RSP_SIZE, rxbuf); 
}

